<template>
    <view class="content">
		<!-- #ifdef MP-WEIXIN -->
		<web-view :src="datainfo.url" />
		<!-- #endif -->
		<!-- #ifdef H5 -->
		<iframe :src="datainfo.url" frameborder="0" width="100%" height="100%"></iframe>
		<DragHomeButton />
		<!-- #endif -->
        <uni-popup ref="popup" type="center" :is-mask-click="false" border-radius="10px">
            <view class="tui-modal-custom">
				<uni-icons type="info-filled" size="60" color="#007AFF"></uni-icons>
				<view class="shenhe-end-desc">
					{{ register_auto === 2 ? '您已提交申请，待团长审核通过后即可进入' : register_auto === 3 ? '团长拒绝了您的申请' : '请从团长的邀请链接中进入' }}
				</view>
			</view>
        </uni-popup>
        <uni-popup ref="popupref" type="center" :is-mask-click="false"  border-radius="10px">
            <view class="tui-modal-custom" style="padding: 50rpx 30rpx;">
				<uni-icons type="info-filled" size="60" color="#007AFF"></uni-icons>
				<view class="tui-modal-custom-text">
					<view class="wenzi"><text>团长昵称：</text>{{teamInfo.cname||'暂无'}}</view>
					<view class="wenzi"><text>团长ID：</text>{{teamInfo.cid||'暂无'}}</view>
				</view>
				<view class="shenhe-end-desc" style="text-align: left;padding-top: 30rpx;">
					当前卡片不是自己团长的卡片，请通过自己团长的卡片进入
				</view>
			</view>
        </uni-popup>
    </view>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { onLoad, onShareAppMessage } from '@dcloudio/uni-app'
import API_LIVE from '@/api/live'
import { getCache, toast, wechatSdkConfig } from '@/utils';
import { useUserStore } from '@/store/modules/user';
import DragHomeButton from './components/DragHomeButton.vue'
const userStore = useUserStore()
const liveid = ref('')
const inviter_code = ref('')
const datainfo = ref<any>('')
const register_auto = ref<number>(0)
const popup = ref<any>()
const popupref = ref<any>()
const teamInfo = ref<any>({
    cname:'暂无',
    cid:'暂无'
})
const userInfo = ref<any>(userStore.userInfo)
onShareAppMessage(() => {
    return {
        title:datainfo.value.name,
        path:'/pagesA/live/liveDetail?inviter_code='+userInfo.value.inviter_code+'&liveid='+liveid.value+'&type='+uni.getStorageSync('identification'),
        imageUrl:datainfo.value.cover||'/static/logo.png'
    };
})

onLoad((options:any) => {
    liveid.value = options.liveid
    inviter_code.value = options.inviter_code
    getDetail()
})

const getDetail =  async () => {
    const res = await API_LIVE.auditStatus()
    if(res.register_auto != 1){
        register_auto.value = res.register_auto
        popup.value.open()
        return
    }
    await API_LIVE.roomDetail({
		zhibo_id: liveid.value,
        invite_code:inviter_code.value
	}).then(async (res:any) => {
        datainfo.value = res.info
		
		// #ifdef H5
		const option ={
			shareTitle: datainfo.value.name,
			shareLink: location.href?.split('#')[0]+'#/pagesA/live/liveDetail?inviter_code='+userInfo.value.inviter_code+'&liveid='+datainfo.value.id,
			shareImage: datainfo.value.cover,
			shareDesc:'直播进行中,' + userInfo.value.nickname+'邀请您一起观看直播'
		}
		wechatSdkConfig(option)
		// #endif
		
	}).catch(async (err:any) => {
		console.log(err)
        const aaa  =await API_LIVE.verifyInviteCode({
			invite_code:inviter_code.value,
			token:getCache('token')
		})
		if(aaa.is_check == 0){
			teamInfo.value = aaa
			popupref.value.open()
			return
		}else{
			toast('直播间不存在')
			setTimeout(() => {
				uni.switchTab({ url: '/pages/user/index' })
			}, 1000);
		}
    })
    if(inviter_code.value){
        await API_LIVE.bindShare({
            room_id: datainfo.value.room_id,
            inviter_code:inviter_code.value
        })
    }
}
</script>
<style scoped lang="scss">
	.content{
		width: 100vw;
		height: 100vh;
		display: flex;
		justify-content: center;
		align-items: center;
	}
.tui-modal-custom {
	text-align: center;width: 600rpx;padding: 50rpx 0;border-radius: 30rpx;
    background:linear-gradient(to bottom,#007AFF,#E7FBF5,#FEFFFF);
	.wenzi {
		width: 400rpx;
		text-align: left;
		margin: 15rpx auto;
		font-size: 30rpx;
		color: #007AFF;
		text {
			display: inline-block;
			width: 160rpx;
			text-align: right;
		}
	}

	.shenhe-end-desc {
		text-align: center;
		font-size: 30rpx;
		padding-top: 50rpx;
		color: #007AFF;
	}
}
</style>